import { trafficInfo, walkInfo } from "../../../../api/drivingSchool"
import { CreatePolyline, PathProps } from "./createPolyline"

interface coords {
	latitude: number
	longitude: number
}

export class Traffic {
	//起点
	startingPoint: coords = {
		latitude: 0,
		longitude: 0,
	}
	//目的地
	destination: coords = {
		latitude: 0,
		longitude: 0,
	}
	//驾车路线
	drivingRoute: any[] = []
	//公交
	busRoute: any[] = []

	constructor() {

	}

	//驾车（driving）路线规划
	onDriving(startingPoint: coords, destination: coords, callback = (arg: any) => { }) {
		this.startingPoint = startingPoint
		this.destination = destination
		wx.request({
			//地图WebserviceAPI驾车路线规划接口 请求路径及参数（具体使用方法请参考开发文档）
			url: `https://apis.map.qq.com/ws/direction/v1/driving/?key=VXABZ-I5TWR-TETWR-WQGYK-Y665O-ZNB5N&from=${startingPoint.latitude},${startingPoint.longitude}&to=${destination.latitude},${destination.longitude}`,
			success: (res: any) => {
				if (Number(res.data.status) === 0) {
					var result = res.data.result
					console.log(res.data)
					const drivingPath: any[] = []

					const cp = new CreatePolyline('driving')
					const route = cp.init(result.routes)
					const pathInfo: any[] = []
					console.log('route', route)
					drivingPath.push(route.result)
					pathInfo.push(route.path)
					callback({ polyline: drivingPath, path: pathInfo, lineCount: result.routes.length })
				} else {
					callback({ polyline: [], path: [], lineCount: 0 })
				}
			}
		})
	}
	//地铁优先（transit）路线规划
	onSubway(startingPoint: coords, destination: coords, callback = (arg: any) => { }) {
		trafficInfo(`${startingPoint.latitude},${startingPoint.longitude}`, `${destination.latitude},${destination.longitude}`, 'RECOMMEND').then(res => {
			console.log(res, res.result === null)
			if (res.result !== null) {
				var result = res.result.result
				//全部线路
				const busPath: any[] = []
				console.log('result.routes', result.routes)
				const pathInfo: any[] = []
				result.routes.forEach((line: any) => {
					const cp = new CreatePolyline('subway')
					const route = cp.init(line.steps)
					console.log('route', route)
					busPath.push(route.result)
					pathInfo.push(route.path)
				})
				callback({ polyline: busPath, path: pathInfo, lineCount: result.routes.length })
			} else {
				//数据返回空，则调用步行线路规划
				console.log('数据返回空，则调用步行线路规划')
				this.onWalk(startingPoint, destination, callback)
			}
			// this.onWalk(startingPoint, destination, callback)
		}, () => {
			callback({ polyline: [], path: [], lineCount: 0 })
		})
	}
	//公交（transit）路线规划
	onBus(startingPoint: coords, destination: coords, callback = (arg: any) => { }) {
		trafficInfo(`${startingPoint.latitude},${startingPoint.longitude}`, `${destination.latitude},${destination.longitude}`, 'LEAST_TIME,NO_SUBWAY').then(res => {
			var result = res.result.result
			console.log(res)
			//全部线路
			const busPath: any[] = []
			console.log('result.routes[0].steps', result.routes[0].steps)
			const pathInfo: any[] = []
			result.routes.forEach((line: any) => {
				const cp = new CreatePolyline('bus')
				const route = cp.init(line.steps)
				console.log('route', route)
				busPath.push(route.result)
				pathInfo.push(route.path)
			})
			callback({ polyline: busPath, path: pathInfo, lineCount: result.routes.length })

		}, () => {
			callback({ polyline: [], path: [], lineCount: 0 })
		}
		)
	}
	//步行（walking）路线规划
	onWalk(startingPoint: coords, destination: coords, callback = (arg: any) => { }) {
		walkInfo(`${startingPoint.latitude},${startingPoint.longitude}`, `${destination.latitude},${destination.longitude}`).then(res => {
			if (res.result !== null) {
				var result = res.result.result
				//全部线路
				console.log('result.routes', result.routes)
				// const path: PathProps = {
				// 	walking: 0, //步行距离
				// 	duration: 0, //时间
				// 	stationCount: 0, //站点
				// 	distance: 0,//全程距离
				// 	geton: '',
				// 	getoff: '',
				// 	lines: [],
				// 	fare: 0,
				// }
				// const polylineArray :any[]=[]
				// const arr: any[] = []
				// result.routes.forEach((line: any) => {
				// 	var route = line
				// 	var coors: any[] = [], pl: any[] = [];
				// 	coors = route.polyline
				// 	var kr = 1#000000;
				// 	for (var i = 2; i < coors.length; i++) {
				// 		coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
				// 	}
				// 	//将解压后的坐标放入点串数组pl中
				// 	for (var i = 0; i < coors.length; i += 2) {
				// 		pl.push({ latitude: coors[i], longitude: coors[i + 1] })
				// 	}
				// 	arr.push({
				// 		points: pl,
				// 		color: '#999',
				// 		width: 6,
				// 		borderColor: '#3cb18a',
				// 		borderWidth: 0,
				// 		arrowLine: true,
				// 		dottedLine: true,
				// 	})
				// 	polylineArray.push(arr)
				// })
				const cp = new CreatePolyline('subway')
				const polylineArray :any[] = cp.walkData( result.routes)

				callback({ polyline: polylineArray, path: [], lineCount: 1 })
			} else {
				//数据返回空
				callback({ polyline: [], path: [], lineCount: 0 })
			}
		}, () => {
			callback({ polyline: [], path: [], lineCount: 0 })
		}
		)
	}
}